$(document).ready(function() {
	//定义变量接收机构数据
	var orgData;
	
	//$("#corpIdCareEndDate").on("click", function(e) {
		//e.stopPropagation();
		//$(this).lqdatetimepicker({
		//	css : 'datetime-day',
			//dateType : 'D',
			//selectback : function() {
			//}
		//});
	//});
	//$("#businessLicenseEndDate").on("click", function(e) {
		//e.stopPropagation();
		//$(this).lqdatetimepicker({
			//css : 'datetime-day',
			//dateType : 'D',
			//selectback : function() {
			//}
		//});
	//});
	//$("#orgEndDate").on("click", function(e) {
		//e.stopPropagation();
		//$(this).lqdatetimepicker({
			//css : 'datetime-day',
			//dateType : 'D',
			//selectback : function() {
			//}
		//});
	//});
	//$("#taxRegCertifEndDate").on("click", function(e) {
	///	e.stopPropagation();
		//$(this).lqdatetimepicker({
		//	css : 'datetime-day',
			//dateType : 'D',
			//selectback : function() {
			//}
		//});
	//});
	//$("#contractSignDate").on("click", function(e) {
		//e.stopPropagation();
		//$(this).lqdatetimepicker({
			//css : 'datetime-day',
			//dateType : 'D',
			//selectback : function() {
			//}
		//});
	//});
	//$("#contractSignBeginDate").on("click", function(e) {
		//e.stopPropagation();
		//$(this).lqdatetimepicker({
		//	css : 'datetime-day',
			//dateType : 'D',
			//selectback : function() {
			//}
		//});
	//});
	//$("#contractSignEndDate").on("click", function(e) {
		//e.stopPropagation();
		//$(this).lqdatetimepicker({
			//css : 'datetime-day',
			//dateType : 'D',
			//selectback : function() {
			//}
		//});
	//});
	//$("#comBuildingDate").on("click", function(e) {
		//e.stopPropagation();
		//$(this).lqdatetimepicker({
			//css : 'datetime-day',
			//dateType : 'D',
			//selectback : function() {
			//}
		//});
	//});
	
	var org = $(".organization");
	var tr = $(".trench");
	var te = $(".tenement");
	var pids = $(".orgPIds");
	
	$.ajax({    
	    url:"../organization/findOrgList.do",    
	    data:"",    
	    type:"GET",    
	    cache:false,    
	    dataType:"text",
	    async:false, //默认为true 异步 
	    success:function(data) {
	    	//替换数字为字符串(js数字类型存储java long类型 64位, 过长时会丢失精度)
	    	data = data.replace(/orgId\":(\d+),/g,"orgId\":\"$1\",");
	    	data = JSON.parse(data);
	    	orgData = data;
	       $.each(data, function(i, item) {
	    	   if(item.orgType == 1){
	    		   if(org.is("select")){
	    			   org.append('<option value="'+item.orgId+'" label="'+item.orgPIds+'">'+item.orgName+'</option');
	    		   }else if(item.orgId == org.attr("dir")){
	    			   org.text(item.orgName); 
	    		   }
	    	   }else if(item.orgType == 2){
	    		   if(tr.is("select")){
	    			   tr.append('<option value="'+item.orgId+'" label="'+item.orgPIds+'">'+item.orgName+'</option');
	    		   }else if(item.orgId == tr.attr("dir")){
	    			   tr.text(item.orgName); 
	    		   }
	    	   }else if(item.orgType == 3){
	    		   if(te.is("select")){
	    			   te.append('<option value="'+item.orgId+'" label="'+item.orgPIds+'">'+item.orgName+'</option');
	    		   }else if(item.orgId == te.attr("dir")){
	    			   te.text(item.orgName); 
	    		   }
	    	   }else{}
	       });
	     },    
	     error : function(XMLHttpRequest, textStatus, errorThrown) {
	          
  		 }    
	});
	
	org.change(function(){
		pids.val(org.find("option:selected").attr("label")+org.val()+"/");
		tr.html("");
		tr.append('<option value="0" >请选择</option');   
		$.each(orgData, function(i, item) {
			if (item.orgType == 2 && item.orgPId == org.val()) {
				tr.append('<option value="'+item.orgId+'" label="'+item.orgPIds+'">'+item.orgName+'</option');  
			}
			if (org.val() == 0 && item.orgType == 2) {
				tr.append('<option value="'+item.orgId+'" label="'+item.orgPIds+'">'+item.orgName+'</option');   
			}
		});   
	});
	
	tr.change(function(){
		te.html("");
		te.append('<option value="0" >请选择</option');   
		$.each(orgData, function(i, item) {
			if (item.orgType == 3 && item.orgPId == tr.val()) {
				te.append('<option value="'+item.orgId+'" label="'+item.orgPIds+'">'+item.orgName+'</option'); 
				pids.val(item.orgPIds);
			}
			if (tr.val() == 0 && item.orgType == 3) {
				te.append('<option value="'+item.orgId+'" label="'+item.orgPIds+'">'+item.orgName+'</option');   
			}
		});   
	});
	
	if (org.is("select")) {
		var orgv = org.attr("dir");
		var trv = tr.attr("dir");
		var tev = tr.attr("dir");
		if(orgv != undefined){
			org.val(orgv);
			org.change();
		}
		if(trv != undefined){
			tr.val(trv);
			tr.change();
		}
		if(orgv != undefined && trv != undefined){
			te.val(tev);
		}
	}
	
	
	
});

//判断图片是否上传
function isNone()
{
	var tt = true;
	$("form div.required").each(function(){
		var id = $(this).attr("id")+"-error";
		if($(this).html().trim() == "")
		{
			if($("#"+id).html() == undefined){
				$(this).parent().append("<div id='"+id+"' class='div-error'>图片不能为空</div>");
			}else{
				$("#"+id).attr("style","display: inline");
			}
			tt = false;
		}else{
			$("#"+id).attr("style","display: none");
		}
    });
	if(!$("form").valid())
	{
		tt = false;
	}
	return tt;
}

(function($){
	$.fn.serializeJson=function(){ 
        var serializeObj={}; // 目标对象 
        var array=this.serializeArray(); // 转换数组格式 
        // var str=this.serialize(); 
        $(array).each(function(){ // 遍历数组的每个元素 {name : xx , value : xxx} 
            if(serializeObj[this.name]){ // 判断对象中是否已经存在 name，如果存在name 
                  if($.isArray(serializeObj[this.name])){ 
                         serializeObj[this.name].push(this.value); // 追加一个值 hobby
                  }else{ 
                          // 将元素变为 数组 ，hobby
                          serializeObj[this.name]=[serializeObj[this.name],this.value]; 
                  } 
            }else{ 
                    serializeObj[this.name]=this.value; // 如果元素name不存在，添加一个属性 name:value 
            } 
        }); 
        return serializeObj; 
	};   
})(jQuery)
